package controllers;

import com.google.gson.JsonObject;
import constants.TplPath;
import constants.system.MemberShip;
import constants.ws.PageBean;
import duppentity.dto.bu.BuDtoUtil;
import duppentity.dto.bu.StationDto;
import duppentity.dto.user.UserDtoUtil;
import duppentity.dto.user.UserOrderDto;
import utils.JsonUtils;
import utils.Utils;

import java.util.List;

/**
 * @author yangz
 * @desc 洗车订单相关功能
 */
public class OrderController extends ApiController {

    /**
     * 洗车订单列表
     */
    public static void orderList(){
        String page = TplPath.orderList;
        if (Utils.exitFile(page)){
            renderTemplate(page);
        }
    }

    public static void getOrderList() {
        PageBean pageBean = PageBean.get();
        String mobile = params.get("mobile",String.class);
        String license = params.get("license",String.class);
        String orderNo = params.get("orderNo",String.class);
        Long userId = params.get("userId",Long.class);
        Integer status = params.get("status",Integer.class);
        Integer refundStatus = params.get("refundStatus",Integer.class);
        MemberShip memberShip = MemberShip.current();

        JsonObject data = userDupp.getUserOrderList(mobile,license,orderNo,userId,status,refundStatus,memberShip.getId(),pageBean.getOffset(),pageBean.pageCount());
        if (!checkResult(data)) {
            apiError(data);
        }
        List<UserOrderDto> userOrderDtoList = UserDtoUtil.getUserOrderDtoList(data);
        apiList(data, userOrderDtoList);
    }



    public static void orderDetail(){
        String page = TplPath.detail;
        Long id = params.get("id",Long.class);
        Long userId = params.get("userId",Long.class);
        JsonObject result = null;
        if (id>0) {
            result =userDupp.getUserOrder(id,userId);
        }
        if (result!=null) {
            UserOrderDto userOrder = UserDtoUtil.getUserOrderDto(result);
            if (Utils.exitFile(page)) {
                renderTemplate(page,userOrder);
            }
        }
    }

    public static void audit(){
        String page = TplPath.audit;
        Long id = params.get("id",Long.class);
        Long userId = params.get("userId",Long.class);
        JsonObject result = null;
        if (id>0) {
            result =userDupp.getUserOrder(id,userId);
        }
        if (result!=null) {
            UserOrderDto userOrder = UserDtoUtil.getUserOrderDto(result);
            if (Utils.exitFile(page)) {
                renderTemplate(page,userOrder);
            }
        }
    }

    /**
     * 修改退款审核
     */
    public static void savAudit() {
        Long id = params.get("id", Long.class);
        Long orderUserId = params.get("orderUserId", Long.class);
        Integer refundAudit = params.get("refundAudit", Integer.class);
        if (id > 0) {
            JsonObject data = userDupp.saveUserOrder(id, MemberShip.current().getSessionId(), MemberShip.current().getId(), orderUserId, refundAudit, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("result").getAsBoolean()) {
                    apiOk("退款审核成功");
                } else {
                    apiError("退款审核失败");
                }
            } else {
                apiError(data);
            }
        }
    }
}
